package org.zfes.snowier.cloud.zbss.pmp.service.impl;

import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.zfes.snowier.core.data.ComboboVo;
import org.zfes.snowier.core.data.DataSet;
import org.zfes.snowier.core.exceptions.ServiceLogicalException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.zfes.snowier.core.util.ZBeanUtil;
import org.zfes.snowier.core.util.ZDateUtil;
import org.zfes.snowier.core.util.ZAlert;
import org.zfes.snowier.core.util.ZAssert;
import org.zfes.snowier.common.dao.params.ParamMap;
import org.zfes.snowier.cloud.zbss.pmp.service.IPmpCloudMemberService;
import org.zfes.snowier.cloud.zbss.pmp.service.IPmpDeduceLineService;
import org.zfes.snowier.cloud.zbss.pmp.dao.PmpDeduceLineMapper;
import org.zfes.snowier.cloud.zbss.pmp.dto.PmpDeduceLineCreateDto;
import org.zfes.snowier.cloud.zbss.pmp.dto.PmpDeduceLineUpdateDto;
import org.zfes.snowier.cloud.zbss.pmp.model.PmpCloudMember;
import org.zfes.snowier.cloud.zbss.pmp.model.PmpDeduceLine;
import java.util.stream.Stream;
@Service
public class PmpDeduceLineServiceImpl implements IPmpDeduceLineService{
 	@Autowired
	private PmpDeduceLineMapper pmpDeduceLineMapper;
	
	@Autowired
	private IPmpCloudMemberService pmpCloudMemberService;
 	@Transactional(rollbackFor=Exception.class)
 	@Override
	public 	void createDeduceLine (Long creatorId,PmpDeduceLineCreateDto deduceLineCreateDto ) {
		ZBeanUtil.validateBean(deduceLineCreateDto);
		PmpDeduceLine pmpDeduceLine=new PmpDeduceLine();
		ZBeanUtil.copy(deduceLineCreateDto, pmpDeduceLine, true);
		
		 ZAssert.bigThanZero(deduceLineCreateDto.getDeduceScore(), "扣分数要大于0", "扣分数要大于0");
	      PmpCloudMember member=pmpCloudMemberService.loadPmpCloudMemberById(creatorId).orElseThrow(()->new ServiceLogicalException("未查询到该用户"));
	      ZAssert.equals(member.getStatus(), Byte.valueOf("1"), "用户状态错误");

	      pmpDeduceLine.setCreatorUserId(creatorId);
	      pmpDeduceLine.setCreatorUserName(member.getUserName());
	        
		createPmpDeduceLine(pmpDeduceLine) ;
	}

	@Transactional(rollbackFor=Exception.class)
 	@Override
	public void updateDeduceLine(PmpDeduceLineUpdateDto deduceLineUpdateDto) {
		ZBeanUtil.validateBean(deduceLineUpdateDto);
		 ZAssert.bigThanZero(deduceLineUpdateDto.getDeduceScore(), "扣分数要大于0", "扣分数要大于0");
		 
		 PmpDeduceLine pmpDeduceLine= loadPmpDeduceLineById(deduceLineUpdateDto.getId()).orElseThrow(()->ZAlert.newSLE("未查询到数据"));
		 ZBeanUtil.copy(deduceLineUpdateDto, pmpDeduceLine, true);
		 
		 updatePmpDeduceLine( pmpDeduceLine);
	}
	@Transactional(rollbackFor=Exception.class)
 	@Override
	public void createPmpDeduceLine(PmpDeduceLine pmpDeduceLine) {
		ZBeanUtil.setCreateInfo(pmpDeduceLine);
		ZBeanUtil.validateBean(pmpDeduceLine);
		pmpDeduceLineMapper.insertSelective(pmpDeduceLine);
	}
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public Optional<PmpDeduceLine> loadPmpDeduceLineById(Long id) {
		if(id!=null){
			return Optional.ofNullable(pmpDeduceLineMapper.selectByPrimaryKey(id));
		}
		return Optional.empty();
		
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void updatePmpDeduceLine(PmpDeduceLine pmpDeduceLine) {
		ZBeanUtil.setUpdatedInfo(pmpDeduceLine);
		ZBeanUtil.validateBean(pmpDeduceLine);
		pmpDeduceLineMapper.updateByPrimaryKeySelective(pmpDeduceLine);
	}
	
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void deletePmpDeduceLine(Long id) {
		ZAssert.notNull(id, "请选择要操作的数据");
		pmpDeduceLineMapper.deleteByPrimaryKey(id);
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void setPmpDeduceLineStatus(Long id, Byte status) {
		ZAssert.notNull(id, "请选择要操作的数据");
		ZAssert.notNull(status, "状态参数错误");
		if(!Stream.of(new Byte[] {-1,0,1}).anyMatch(val->val.equals(status))) {
			ZAlert.serviceLogicalError("状态参数错误");
		}
		pmpDeduceLineMapper.updateStatus( id,  status) ;
	}
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public DataSet loadPmpDeduceLineDataSet(Map<String, Object> params) {
		ParamMap pm=ParamMap.filterParam(params);
		if(!pm.getLong("projectId").isPresent()) {
			return DataSet.emptyDS();
		}
		DataSet ds=DataSet.newDS2(pmpDeduceLineMapper.selectListCount(pm), pmpDeduceLineMapper.selectMapListPage(pm));
		return ds;
	}
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public List<ComboboVo> loadComboboVo(Long projectId,boolean firstEmpty) {
		List<ComboboVo> voList=pmpDeduceLineMapper.selectComboboVo(projectId);
		if(firstEmpty) {
			voList.add(0, ComboboVo.newEmptyFirst());
		}
		return voList;
	}
	
}